//
//  iRSocket.m
//  iRSocketTest
//
//  Created by 胡鹏飞 on 15/8/7.
//  Copyright (c) 2015年 胡鹏飞. All rights reserved.
//

#import "iRSocket.h"
@implementation iRSocket

+ (iRSocket *)defaultSocket
{
    static iRSocket *socket = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        socket = [[self alloc] init];
    });
    return socket;
}

- (id)init
{
    self = [super init];
    if (self) {
        _socket = [[AsyncSocket alloc] initWithDelegate:self];
        _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}

- (BOOL)connectTheServerWithIP:(NSString *)ip andPortNum:(int)port
{
    if (!_socket.isConnected){
        _Ip = [NSString stringWithString:ip];
        _port = port;
        
        NSError *err = nil;
        BOOL isc = [_socket connectToHost:ip onPort:port error:&err];
        
        if (!isc){
            // 连接失败
            if(_delegate){
                [_delegate connectFailedWithError:[[NSError alloc] initWithDomain:@"The connection fails" code:98 userInfo:nil]];
            }
            _isConnecting = NO;
            return NO;
        }else{
            NSLog(@"_socket.isConnected = %d", _socket.isConnected);
            _isConnecting = YES;
            if (_delegate) {
                [_delegate connectSuccess:self];
            }
            
            return YES;
        }
    }
    return YES;
}

- (void)dissConnect
{
    if (_socket.isConnected) {
        [_socket disconnect];
    }
    _Ip = nil;
    _isConnecting = NO;
    if (_delegate) {
        [_delegate sockeDisconnected:self];
    }
}

- (void)sendMessage
{
    if (_isRequesting == NO){
        if ([_socket isConnected]) {
            for (int i=0; i<16; i++)
            {
                int bb [1] ;
                //                   bb[0]= aa[i];
                bb[0] = [[_dataSource objectAtIndex:i] intValue];
                
                NSData *data = [[NSData alloc] initWithBytes:bb length:1];
                [_socket writeData:data withTimeout:-1 tag:0];
                _isRequesting = YES;
            }
            [_socket readDataWithTimeout:-1 tag:0];
        }else{
            if (_delegate) {
                NSError *err = [[NSError alloc] initWithDomain:@"当前处于未连接状态" code:401 userInfo:nil];
                [_delegate sendMessageFailedWithError:err];
            }
        }
    }
}

#pragma AsyncSocket delegate
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    _isRequesting = NO;
    if (_delegate) {
        [_delegate receivedMessage:data];
    }
}

- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    _isRequesting = NO;
    if (_delegate) {
        [_delegate sockeDisconnected:self];
    }
}

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
    if (_delegate) {
        [_delegate sendMessageSuccessed:self];
    }
}

//- (void)sendMessage:(NSString *)contentString
//{
//    if ([_socket isConnected]) {
//        //[_socket set]
//        NSData *data = [contentString dataUsingEncoding:NSISOLatin1StringEncoding];
//        [_socket writeData:data withTimeout:0 tag:1];
//        [_socket readDataWithTimeout:0 tag:1];
//    }
//    
//}

/*
- (void)sendMessageWIthData:(NSData*)contentData
{
    if ([_socket isConnected]) {
        //[_socket set]
        [_socket writeData:contentData withTimeout:0 tag:1];
        [_socket readDataWithTimeout:0 tag:1];
    }
}
 */
@end
